﻿using MySchool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //  数据绑定
            DBHelper dBHelper = new DBHelper();
            // 创建数据集对象
            DataSet ds = new DataSet();
            dBHelper.OpenConnection();
            string sql = "select * from sys_user";
            SqlDataAdapter adapter = new SqlDataAdapter(sql, dBHelper.Connection);
            // 数据填充
            adapter.Fill(ds, "sys_user");

            // 完善数据
            DataRow row = ds.Tables["sys_user"].NewRow();
            row[0] = -1;
            row[1] = "全部";
            ds.Tables["sys_user"].Rows.InsertAt(row, 0);

            // 和下拉列表关联数据
            this.comboBox1.DataSource = ds.Tables["sys_user"];
            // 绑定实际值
            this.comboBox1.ValueMember = "Id";
            // 绑定显示的值
            this.comboBox1.DisplayMember = "username";

           


            dBHelper.CloseConnection();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show(this.comboBox1.SelectedValue.ToString());
        }
    }
}
